package com.chinatelecom.clientservice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinatelecom.clientservice.commons.SystemStatusConstants;
import com.chinatelecom.clientservice.pojo.BaseUsers;
import com.chinatelecom.clientservice.pojo.ExtReturn;
import com.chinatelecom.clientservice.pojo.Tree;
import com.chinatelecom.clientservice.service.SystemService;
import com.google.gson.Gson;

@Controller
public class SystemController {
	@Autowired
	private SystemService systemService;

	@RequestMapping(value = "/loginPage")
	public String home() {
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public ExtReturn login(@RequestParam String account, @RequestParam String password, HttpSession session,
			HttpServletRequest request) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("account", account);
		params.put("password", password);
		BaseUsers loginUsers = systemService.selectByBaseUser(params);
//		Gson gson = new Gson();
		if (loginUsers != null) {
			session.setAttribute(SystemStatusConstants.CURRENT_USER, loginUsers);
//			return gson.toJson(new ExtReturn(true, "success"));
			return new ExtReturn(true,"success");
		} else {
//			return gson.toJson(new ExtReturn(false, "用户名或者密码错误!"));
			return new ExtReturn(false, "用户名或者密码错误!");
		}
	}

	@RequestMapping(value = "/main")
	public String main() {
		return "main";
	}

	@RequestMapping(value = "/header")
	public String header() {
		return "header";
	}

	@RequestMapping(value = "/treeMenu")
	@ResponseBody
	public List<Tree> treeMenu() {
		Tree treeMenu = systemService.selectMenuByUser(null);
		return treeMenu.getChildren();
	}

	@RequestMapping(value = "/welcome")
	public String welcome() {
		return "welcome";
	}
}
